﻿using System;
using System.Windows.Forms;
using FastNET.CleanAct.Client.ManagerEngineBusiness;
using FastNET.CleanAct.Common.Configuration.Actions.RegistryActions;
using FastNET.CleanAct.Common.Core;
using Microsoft.Win32;

namespace FastNET.CleanAct.Tools.HomeTemplateEditor.ActionDesigners
{
    internal partial class RegistryActionCreator : Form
    {
        public RegistryActionCreator()
        {
            InitializeComponent();

            RecursiveModeBox.DataSource = Enum.GetValues(typeof(ActionRecursiveMode));
            ValueCreateType.DataSource = Enum.GetValues(typeof(RegistryValueKind));

            LoadSettings(null);
        }

        private void RegistryActionCreator_Load(object sender, EventArgs e)
        {
            CheckValueFieldStates();
        }

        #region Public Action Properties

        public ActionRecursiveMode ActionRecursiveMode
        {
            get
            {
                if (RecursiveModeBox.SelectedIndex <= -1)
                    return ActionRecursiveMode.None;

                return (ActionRecursiveMode)RecursiveModeBox.SelectedItem;
            }

            set
            {
                RecursiveModeBox.SelectedItem = value;
            }
        }

        public string ActionName
        {
            get
            {
                return NameField.Text;
            }

            set
            {
                NameField.Text = value;
            }
        }

        public string ActionValue
        {
            get
            {
                return SaveSettings();
            }

            set
            {
                LoadSettings(value);
            }
        }

        #endregion

        #region Load/Unload

        private string SaveSettings()
        {
            RegistryActionSettings settings = new RegistryActionSettings();

            if (TargetAll.Checked) settings.Target = ActionTarget.All;
            if (TargetKey.Checked) settings.Target = ActionTarget.Key;
            if (TargetValues.Checked) settings.Target = ActionTarget.Value;

            if (ScopeUser.Checked) settings.Scope = ActionScope.User;
            if (ScopeComputer.Checked) settings.Scope = ActionScope.Computer;

            if (ValueDelete.Checked) settings.Action = ActionType.Delete;

            if (ValueCreate.Checked)
            {
                settings.Action = ActionType.Create;
                settings.Value = ValueCreateField.Text;
            }

            if (ValueEdit.Checked)
            {
                settings.Action = ActionType.Modify;
                settings.Value = ValueEditField.Text;
            }

            settings.Pattern = PatternField.Text;
            settings.Path = PathField.Text;

            return RegistryActionSettings.WriteXML(settings);
        }

        private void LoadSettings(string xml)
        {
            ValueCreateField.Text = null;
            ValueEditField.Text = null;
            ValueCreateType.SelectedIndex = -1;
            TargetAll.Checked = false;
            TargetValues.Checked = false;
            TargetKey.Checked = false;
            ScopeUser.Checked = false;
            ScopeComputer.Checked = false;
            ValueCreate.Checked = false;
            ValueDelete.Checked = false;
            ValueEdit.Checked = false;

            if (String.IsNullOrEmpty(xml))
                return;

            RegistryActionSettings settings = RegistryActionSettings.LoadXml(xml);

            switch (settings.Action)
            {
                case ActionType.Create:
                    if (!String.IsNullOrEmpty(settings.ValueType))
                    {
                        ValueCreateType.SelectedValue = settings.ValueType;
                    }

                    ValueCreateField.Text = settings.Value;
                    ValueCreate.Checked = true;
                    break;

                case ActionType.Modify:
                    ValueEditField.Text = settings.Value;
                    ValueEdit.Checked = true;
                    break;

                case ActionType.Delete:
                    ValueDelete.Checked = true;
                    break;
            }

            PatternField.Text = settings.Pattern;
            PathField.Text = settings.Path;

            switch (settings.Target)
            {
                case ActionTarget.All:
                    TargetAll.Checked = true;
                    break;

                case ActionTarget.Key:
                    TargetKey.Checked = true;
                    break;

                case ActionTarget.Value:
                    TargetValues.Checked = true;
                    break;
            }

            switch (settings.Scope)
            {
                case ActionScope.User:
                    ScopeUser.Checked = true;
                    break;

                case ActionScope.Computer:
                    ScopeComputer.Checked = true;
                    break;
            }
        }

        #endregion

        #region Checks

        private void CheckSave()
        {
            Save.Enabled = true;

            if (!ValueEdit.Checked && !ValueDelete.Checked && !ValueCreate.Checked)
            {
                Save.Enabled = false;
            }

            if (!ScopeUser.Checked && !ScopeComputer.Checked)
            {
                Save.Enabled = false;
            }

            if (!TargetAll.Checked && !TargetKey.Checked && !TargetValues.Checked)
            {
                Save.Enabled = false;
            }
        }

        private void CheckValueFieldStates()
        {
            ValueCreateType.Enabled = false;
            ValueCreateField.Enabled = false;
            ValueEditField.Enabled = false;

            if (ValueCreate.Checked)
            {
                ValueCreateType.Enabled = true;
                ValueCreateField.Enabled = true;
            }

            if (ValueEdit.Checked)
            {
                ValueEditField.Enabled = true;
            }
        }

        #endregion

        #region Radio Events

        private void ValueCreate_CheckedChanged(object sender, EventArgs e)
        {
            CheckValueFieldStates();
            CheckSave();
        }

        private void ValueEdit_CheckedChanged(object sender, EventArgs e)
        {
            CheckValueFieldStates();
            CheckSave();
        }

        private void ValueDelete_CheckedChanged(object sender, EventArgs e)
        {
            CheckValueFieldStates();
            CheckSave();
        }

        private void TargetAll_CheckedChanged(object sender, EventArgs e)
        {
            CheckSave();
        }

        #endregion

        private void TargetValues_CheckedChanged(object sender, EventArgs e)
        {
            CheckSave();
        }

        private void TargetKey_CheckedChanged(object sender, EventArgs e)
        {
            CheckSave();
        }

        private void ScopeUser_CheckedChanged(object sender, EventArgs e)
        {
            CheckSave();
        }

        private void ScopeComputer_CheckedChanged(object sender, EventArgs e)
        {
            CheckSave();
        }
    }
}
